import Cookies from "js-cookie";
//获取LocalStorage
export const getLocalStorage = (key) => {
  if (!key) return null;
  try {
    return JSON.parse(localStorage.getItem(key));
  } catch (error) {
    return Cookies.get(key);
  }
};
// 存入LocalStorage
export const setLocalStorage = (key, value) => {
  if (!key) return null;

  if (typeof value === "object") {
    value = JSON.stringify(value);
  }
  return localStorage.setItem(key, value);
};
// 删除LocalStorage
export const removeLocalStorage = (key) => {
  if (!key) return null;
  return localStorage.removeItem(key);
};

// 获取Cookies
export const getCookies = (key) => {
  if (!key) return null;
  try {
    return JSON.parse(Cookies.get(key));
  } catch (error) {
    return Cookies.get(key);
  }
};
// 存入Cookies
export const setCookies = (key, value, expires = 7) => {
  if (!key) return null;

  if (typeof value === "object") {
    value = JSON.stringify(value);
  }
  return Cookies.set(key, value, { expires });
};
// 删除Cookies
export const removeCookies = (key) => {
  if (!key) return null;
  return Cookies.remove(key);
};

// 清空登录信息
export const clearLoginInfo = () => {
  removeCookies("TOKEN");
  removeLocalStorage("USER_INFO");
};
